<template>
  <div class="detail">
    <div class="img">
      <img :src="imgUrl" alt="" />
    </div>
    <div class="banner">
      <button class="prev" @click="prevImage">&lt;</button>
      <div class="img_box">
        <img
          v-for="(pic, index) in picUrl"
          :key="index"
          :src="pic"
          @click="toggle(index)"
          :class="{ active: currentIndex === index }"
        />
      </div>
      <button class="next" @click="nextImage">&gt;</button>
    </div>
    <p>{{ '商品编号:' + bn }}</p>
  </div>
</template>

<script>
export default {
  created() {
    // console.log(this.goods)
    // console.log(this.bn)
  },
  props: ['goods', 'bn'],
  data() {
    return {
      picUrl: this.goods.picList
        ? this.goods.picList.map((item) => item.picUrl)
        : [],
      currentIndex: 0
    }
  },
  computed: {
    imgUrl() {
      return this.picUrl[this.currentIndex]
    }
  },
  methods: {
    toggle(index) {
      this.currentIndex = index
    },
    prevImage() {
      this.currentIndex =
        (this.currentIndex - 1 + this.picUrl.length) % this.picUrl.length
    },
    nextImage() {
      this.currentIndex = (this.currentIndex + 1) % this.picUrl.length
    }
  }
}
</script>

<style scoped>
.detail {
  width: 500px;
  /* border: 1px solid #000; */
}
.img img {
  width: 100%;
  transition: all 0.3s ease;
}
.banner {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  height: 80px;
  line-height: 80px;
  /* border: 1px solid #000; */
}
.banner button {
  width: 10%;
  height: 100%;
  font-size: 20px;
  border: 0px solid #000;
  background-color: white;
}
.banner .img_box {
  display: flex;
  width: 80%;
  height: 100%;
  justify-content: space-between;
  /* border: 1px solid #000; */
  /* vertical-align: middle; */
}
.banner .img_box img {
  width: 20%;
  cursor: pointer;
  opacity: 0.6;
  transition: opacity 0.3s ease;
  /* border: 1px solid #000; */
  vertical-align: middle;
  overflow: scroll;
}
.banner img.active {
  opacity: 1;
}
.prev,
.next {
  padding: 20px;
  cursor: pointer;
  border: 1px solid #838383;
}
</style>
